/**
 * 
 */
package edu.co.gap.view.webservice;

import java.io.ByteArrayOutputStream;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import edu.co.gap.view.R;
import edu.co.gap.view.main;

/**
 * @author David
 *
 */
public class webs extends Activity {

	
	//web services atributes
	private static final String METHOD_NAME = "getBytes";
	private static final String SOAP_ACTION = "http://192.168.0.13:5304/Servidor_GAP/services/WebServices/getBytes";
//	private static final String SOAP_ACTION = "http://192.168.0.13:8081/Servidor_GAP/services/WebServices/getbytes";
	private static final String NAMESPACE = "http://webservices.gap.co.edu";
	private static final String URL = "http://192.168.0.13:5304/Servidor_GAP/services/WebServices?wsdl";
//	private static final String URL = "http://192.168.0.13:8081/Servidor_GAP/services/WebServices?wsdl";
	
	private TextView txtState;
	
	  /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webs);
        
        txtState = (TextView) findViewById(R.id.idTxtEstado);  
        
        
        String filepath="";
		filepath = Environment.getExternalStorageDirectory()+"/"+main.IMAGE_NAME;
		
		Bitmap bmp=BitmapFactory.decodeFile(filepath);
		
		String result=testWebService(bmp);
		
		txtState.setText("Status : "+result);
		
    }
    
    
    public String testWebService(Bitmap bmp) {

	    MarshalBase64 marshal = new MarshalBase64();
	    ByteArrayOutputStream out = new ByteArrayOutputStream();
	    bmp.compress(CompressFormat.JPEG, 100, out);
	    byte[] raw = out.toByteArray();

	    SoapObject request =new SoapObject(NAMESPACE, METHOD_NAME);

	    request.addProperty("image", raw);
	    //test
//	    request.addProperty("format", main.FORMAT);
	    
	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
	        SoapEnvelope.VER11);
	    envelope.setOutputSoapObject(request);
	    marshal.register(envelope);
	    HttpTransportSE httpTransport = new HttpTransportSE(URL);
	    httpTransport.debug=true;
	    try

	    {

	        httpTransport.call(SOAP_ACTION, envelope);
	        
	    	String req=httpTransport.requestDump;
			String res=httpTransport.responseDump;
			
	    	SoapPrimitive sp =(SoapPrimitive)envelope.getResponse();
		
		return sp.toString();
			
	  }

	    catch (Exception exception)

	    {
	        exception.printStackTrace();
	        
	        return exception.getMessage();
	    }
	    
	    }
    
    
    
    
}
